package tool;

import Commer.Count;
import Commer.GlobalMouseClickPosition;
import Commer.MyFrame;

import javax.swing.*;
import java.awt.*;

public class MainUi extends JFrame {

    private IEControl ieControl;

    public MainUi() {
        // 设置关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 窗口大小不可变
        setResizable(false);
        // 窗口可移动
        setAlwaysOnTop(true);
        // 窗口显示屏幕中间
        setBounds(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 250, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 200, 500, 400);
        // 初始化IEControl实例
        ieControl = new IEControl();
    }

    public void start() {
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建文本区域
        JTextArea jTextArea = new JTextArea("->消息自动发送器，点击开始按钮自动打开游览器并跳到抖音网页，开始给指定用户发信息，要求暂停点击结束按钮");
        jTextArea.setEditable(false);
        jTextArea.setLineWrap(true);
        jTextArea.setWrapStyleWord(true);
        jTextArea.setFont(new java.awt.Font("微软雅黑", 0, 34));
        jTextArea.setBackground(new java.awt.Color(255, 255, 255)); 

        JMenuBar jMenuBar = new JMenuBar();
        JMenu jMenu = new JMenu("工具");
        jMenuBar.add(jMenu);

        JMenuItem mouse = new JMenuItem("点击位置");
        JMenuItem in = new JMenuItem("设置位置");
        JMenuItem count = new JMenuItem("设置人数");
        jMenu.setFont(new java.awt.Font("微软雅黑", 0, 24));
        mouse.setFont(new java.awt.Font("微软雅黑", 0, 24));
        in.setFont(new java.awt.Font("微软雅黑", 0, 24));
        count.setFont(new java.awt.Font("微软雅黑", 0, 24));
        //给JMenuBar添加阴影边框
        jMenuBar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        mouse.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        in.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        count.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        mouse.addActionListener(e -> new GlobalMouseClickPosition());
        in.addActionListener(e -> {
            // 创建并显示窗口
            SwingUtilities.invokeLater(MyFrame::new);
        });
        jMenuBar.add(mouse);
        jMenuBar.add(in);
        jMenuBar.add(count);
        count.addActionListener(e -> {
            // 创建并显示窗口
            new Count();
        });



        // 创建滚动条
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        scrollPane.setPreferredSize(new Dimension(500, 100));

        // 创建按钮
        JButton topjButton = new JButton("开始");
        JButton bottomjButton = new JButton("结束");
        topjButton.setFont(new java.awt.Font("微软雅黑", 0, 24));
        bottomjButton.setFont(new java.awt.Font("微软雅黑", 0, 24));

        // 创建包含两个按钮的子面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(topjButton);
        buttonPanel.add(bottomjButton);

        // 添加按钮监听器
        topjButton.addActionListener(e -> {
            //更改窗口位置
            setLocation(0,0);
            // 在新线程中启动IEControl
            new Thread(ieControl::run).start();
        });
        bottomjButton.addActionListener(e -> {
            // 停止IEControl
            ieControl.stop();
        });

        // 将组件添加到主面板
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        mainPanel.add(jMenuBar, BorderLayout.NORTH);

        // 设置窗口大小
        setSize(500, 400);
        // 将主面板添加到窗口
        setContentPane(mainPanel);

        // 显示窗口
        setVisible(true);
    }


}
